package com.cmy.controller;

import com.cmy.pojo.Books;
import com.cmy.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    //controller调用service层
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    //查询全部的书籍,并且返回一个书籍展示界面
    @RequestMapping("/allBook")
    public String list(Model model){

        List<Books> books = bookService.queryAllBooks();
        model.addAttribute("list",books);
        return "allBook";
    }
    //跳转到书籍增加页面
    @RequestMapping("/toAddBook")
    public String toAddBook(){
        return "addBook";
    }
    //添加书籍的请求
    @RequestMapping("/addBook")
    public String addBook(Books books){
        System.out.println("addBook->"+books);
        bookService.addBook(books);
        return "redirect:/book/allBook";
    }

    //跳转到修改页面
    @RequestMapping("/toUpdatePage")
    public String toUpdatePage(int id,Model model){
        Books books = bookService.queryBookById(id);
        model.addAttribute("Qbooks",books);
        model.addAttribute("Qid",id);
        return "updateBook";
    }

    //修改书籍
    @RequestMapping("/updateBook")
    public String updateBook(Books books){
        System.out.println("updateBook->"+books);
        bookService.updateBook(books);
        return "redirect:/book/allBook";
    }

    //删除书籍
    @RequestMapping("/deleteBook/{bookID}")
    public String deleteBook(@PathVariable("bookID") int id){
        bookService.delBookById(id);
        return "redirect:/book/allBook";
    }

    //查询书籍
    @RequestMapping("/qureyBookByName")
    public String queryBook(String queryName,Model model){
        List<Books> books = bookService.queryBookByName(queryName);
        //ArrayList<Books> booklist = new ArrayList<Books>();
        //
        //booklist.add(books);
        model.addAttribute("list",books);
        return "allBook";
    }
}

    